package www.rlicker.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.User;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.IUserService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping(value="/user")
public class UserController extends BaseController{

	@Autowired
	private IUserService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> list = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(list));
		}
		return "user/userList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<User> param = new Criteria<User>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("startTime"))) {
			 param.addParam("startTime", request.getParameter("startTime"));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("endTime"))) {
          	 param.addParam("endTime", request.getParameter("endTime"));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("phone"))) {
        	 param.addParam("phone", request.getParameter("phone"));
         }
 	     if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	     }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	     }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	@RequestMapping("/detail")
	public String detail(String id, ModelMap map){
		map.put("user", service.queryEntity(new Long(id)));
		return "user/userDetail";
	}
	
	@ResponseBody
	@RequestMapping(value="/update" , method = { RequestMethod.POST })
	public JsonResult update(HttpServletRequest request) throws ParseException{
		JsonResult jr = JsonResult.newInstance(); 
		String content ="";
		Criteria<User> param = new Criteria<User>();
		User user = new User();
        if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
       	   user.setStatus(request.getParameter("status"));
	       	if (request.getParameter("status").equals(STR_0)) {
				content ="恢复了"+request.getParameter("name")+"用户";
			}else {
				content ="封号了"+request.getParameter("name")+"用户";
			}
        }
        if (!StringUtil.isNullOrBlank(request.getParameter("vipEndTime"))) {
        	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	user.setVipEndTime(format.parse(request.getParameter("vipEndTime")));
        }
        if (!StringUtil.isNullOrBlank(request.getParameter("isVip"))) {
        	user.setIsVip(request.getParameter("isVip"));
        }
		param.setRecord(user);
		param.addParam("id", request.getParameter("id"));
	    String ret = service.update(param);
	    if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
	    	insertLog(content);
	    }
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
}
